<?php

require_once 'classes/LightPressPlugin.php';

class TagCloud extends LightPressPlugin {

    var $default_context = LP_CONTEXT_ALL;
    var $active = true;
    var $hooks = array('post_render');

    var $description = 'Displays a tag cloud (or cosmos/heatmap/listing)';
    var $constructor_args = array(
        'title'             => 'Title to display before tags',
        'display_max'       => 'Maximum number of tags to display',
        'steps'             => 'Odd number of scaling steps to use',
        'size_min'          => 'Minimum font size to use',
        'size_max'          => 'Maximum font size to use',
        'size_unit'         => 'Css font size unit',
        'skip_single'       => 'Do not display tags containing only one post',
        'show_numposts'     => 'Show number of posts next to each tag'
        );
    
    var $_frontend;
	
    // options
    var $title = "Tag cloud";
    var $display_max = 30;
    var $steps = 5;
    var $size_min = 0.5;
    var $size_max = 1.5;
    var $size_unit = 'em';
    var $skip_single = false;
    var $show_numposts = false;

    function TagCloud(&$frontend, $args, $dummy_run=false) {
		
        $this->LightPressPlugin($frontend, $args, $dummy_run);
        
        if (!$dummy_run && $frontend->view_type != 'html')
            $this->active = false;
        
	}
    
    function run($hook, &$post) {
        if (!$this->active)
            return ($this->hide());
        
        $tags = $this->_frontend->getTags();

        $limit = (int)$this->display_max;
        if ($limit > 0 && count($tags) > $limit)
            $tags = array_slice($tags, 0, $limit);
        
        ksort($tags);

        $min = 9999;
        $max = 0;
        foreach ($tags as $tag_name=>$tag) {
            $numposts = (int)$tag['numposts'];
            if ($this->skip_single && $numposts <= 1) {
                unset($tags[$tag_name]);
                continue;
            }
            $tags[$tag_name]['numposts'] = $numposts;
            if ($numposts > $max)
                $max = $numposts;
            else if ($numposts < $min)
                $min = $numposts;
        }
        
        $dist = ($max - $min) / $this->steps;
        $step = ($this->size_max - $this->size_min) / $this->steps;
        if ($this->title)
            $buffer = "<h2>{$this->title}</h2>\n";
        else
            $buffer = '';
        $buffer .= "<ul id=\"tagcloud_lite\" class=\"cloud\">\n";
        //echo "max $max min $min dist $dist step $step<br />";
        $url = $this->_frontend->options['url'];
        if ($this->show_numposts) {
            foreach ($tags as $tag_name=>$tag) {
                $buffer .= sprintf(
                    '<li><span style="font-size:%02f%s"><a href="%s/%s">%s</a> (%s)</span></li>',
                    $this->size_min + floor($tag['numposts'] / $dist) * $step, $this->size_unit, $url, $tag['permalink'], $tag['name'], $tag['numposts']);
                $buffer .= "\n";
            }
        } else {
            foreach ($tags as $tag_name=>$tag) {
                $buffer .= sprintf(
                    '<li><a href="%s/%s" style="font-size:%02f%s">%s</a></li>',
                    $url, $tag['permalink'], $this->size_min + floor($tag['numposts'] / $dist) * $step, $this->size_unit, $tag['name']);
                $buffer .= "\n";
            }
        }
        $buffer .= '</ul>';
        $this->_frontend->tpl->setVar('PLUGIN_TAGCLOUD', $buffer);
	}
    
	function hide() {
        $this->_frontend->tpl->setVar('PLUGIN_TAGCLOUD', '');
    }
}

?>
